﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.PackageManager.UI;
using UnityEngine;
using XFABManager;

namespace XFGameFramework
{
    public class CreateModuleCommand : EditorWindow
    {

        private const string ModuleTemplate = @"
using System; 
using System.Collections.Generic; 
using XFGameFramework;

public class {ModuleName} : Module
{ 
    // 当前模块所使用的资源模块名称
    public override string ProjectName => {ProjectName};
     
    // 是否为基础模块，基础模块不可关闭，如果模块能够关闭，请设置为false
    public override bool BaseModule => {BaseModule};

    // 默认启动的场景的名称，如果不需要启动场景保持默认即可
    public override string DefaultStartUpScene => {DefaultStartUpScene};

    // 需要预加载的表(仅支持json) key:json文件名称 value:类型
    public override Dictionary<string, Type> PreloadConfigTables => base.PreloadConfigTables;

    // 当模块启动时触发
    public override void OnStart()
    {
        UnityEngine.Debug.Log(""OnStart:""+ModuleName);
    }
}
";

        private GUIContent moduleNameContent = new GUIContent("ModuleName", "模块名称");
        private GUIContent projectNameContent = new GUIContent("ProjectName", "该模块使用的资源模块名称!");
        private GUIContent baseModuleContent = new GUIContent("BaseModule", "是否为基础模块,基础模块不能退出，如果该模块能关闭或退出，请设置为false!");
        private GUIContent defaultStartUpSceneContent = new GUIContent("DefaultStartUpScene", "默认启动的场景,如果不需要默认启动场景,设置为空即可!");

        private GUIContent emptyNotify = new GUIContent("ModuleName或ProjectName不能为空!");

        private string moduleName;
        private string projectName;
        private bool baseModule;
        private string defaultStartUpScene;

        [MenuItem("Assets/Create/XFKT/XFGameFramework/CreateModule")]
        static void CreateModuleItem()
        {  
            Rect rect = new Rect(0, 0, 350, 270);
            CreateModuleCommand window = GetWindowWithRect<CreateModuleCommand>(rect, true, "创建模块");
            window.Show(); 
        }



        private void OnGUI()
        {
            GUILayout.Space(20);
            moduleName = EditorGUILayout.TextField(moduleNameContent, moduleName);
            GUILayout.Space(20);
            projectName = EditorGUILayout.TextField(projectNameContent, projectName);
            GUILayout.Space(20);
            baseModule = EditorGUILayout.Toggle(baseModuleContent, baseModule);
            GUILayout.Space(20);
            defaultStartUpScene = EditorGUILayout.TextField(defaultStartUpSceneContent, defaultStartUpScene);

            GUILayout.Space(60);

            if (GUILayout.Button("创建模块"))
            {
                if (string.IsNullOrEmpty(moduleName) || string.IsNullOrEmpty(projectName)) {
                    ShowNotification(emptyNotify);
                    return;
                }
                CreateModule(moduleName, projectName, baseModule, defaultStartUpScene);
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
                Close();
            }
        }

        public static void CreateModule(string moduleName, string projectName, bool baseModule, string defaultStartUpScene)
        {
            if (string.IsNullOrEmpty(moduleName) || string.IsNullOrEmpty(projectName))
            {
                Debug.LogError("创建模块失败!moduleName和projectName不能为空!");
                return;
            }
            string path = string.Format("Assets/Games/{0}/{1}.cs",moduleName,moduleName);

            if (AssetDatabase.LoadAssetAtPath<Object>(path) != null)
            {
                Debug.LogErrorFormat("脚本:{0}已经存在!",path);
                return;    
            }

            string dir = System.IO.Path.GetDirectoryName(path);
            if (!System.IO.Directory.Exists(dir))
                System.IO.Directory.CreateDirectory(dir);

            string content = ModuleTemplate.Replace("{ModuleName}",  moduleName);
            content = content.Replace("{ProjectName}", string.Format("\"{0}\"", projectName));
            content = content.Replace("{BaseModule}", baseModule.ToString().ToLower());
            content = content.Replace("{DefaultStartUpScene}", string.IsNullOrEmpty(defaultStartUpScene) ? "string.Empty" : string.Format("\"{0}\"", defaultStartUpScene) );

            System.IO.File.WriteAllText(path, content);
        }

    }
}


